package com.swind.fast.modules.app.space.rent.component;

import java.net.URL;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;

import com.swind.fast.common.util.oConvertUtils;

import org.springframework.stereotype.Component;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.swind.fast.modules.app.space.rent.event.SpaceSyncAnnounceEvent;

import com.swind.fast.modules.app.space.rent.entity.SpaceContract;
import com.swind.fast.modules.app.space.rent.service.ISpaceContractService;
import com.swind.fast.modules.app.space.rent.service.ISpaceContractBillService;
import com.swind.fast.modules.app.space.rent.vo.SpaceDashboardStatisticVo;

import org.springframework.context.ApplicationContext;

import com.swind.fast.common.system.util.JwtUtil;

import cn.hutool.core.date.DateBetween;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;

import lombok.extern.slf4j.Slf4j;
import java.util.Date;

@Slf4j
@Component
public class SpaceComponent {
    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    ISpaceContractService spaceContractService;

    @Autowired
    ISpaceContractBillService spaceContractBillService;

    // @Scheduled(cron = "0/30 * * * * ?") // cron表达式：每周一 23:40:30 执行
    // public void doTask(){
    //     System.out.println("我是定时任务~");
    //     applicationContext.publishEvent(new SpaceSyncAnnounceEvent(""));
    // }

    @Scheduled(cron = "0 0 7,12,17 * * ?")
    public void updateTask(){
        System.out.println("定时刷新提醒操作~");
        applicationContext.publishEvent(new SpaceSyncAnnounceEvent(""));
    }


    public SpaceDashboardStatisticVo getDashboardData() {
        String mdata = spaceContractBillService.getMonthData();
        log.info("mdata: {}", mdata);

        String edata = spaceContractService.getStatisticData();
        log.info("edata: {}", edata);

        SpaceDashboardStatisticVo resp = new SpaceDashboardStatisticVo();
        resp.setCmonth(Convert.toInt(mdata.split(",")[0]).toString());
        resp.setNmonth(Convert.toInt(mdata.split(",")[1]).toString());
        resp.setExpired(Convert.toInt(mdata.split(",")[2]).toString());

        resp.setE30day(Convert.toInt(edata.split(",")[0]).toString());
        resp.setE60day(Convert.toInt(edata.split(",")[1]).toString());
        resp.setDeposit(Convert.toInt(edata.split(",")[2]).toString());

        return resp;
    }

}